# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Dec  2 2011)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc
import webbrowser
import urllib
import threading

import env
from functions import *


###########################################################################
## Class MyFrame
###########################################################################


svn_ver = 0
issue_info = {}
class MyFrame ( wx.Frame ):
    
    t = {}
    
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"测试邮件生成", pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_WINDOW ) )
        
        bSizer3 = wx.BoxSizer( wx.VERTICAL )
        
        box_form_1 = wx.GridBagSizer( 0, 0 )
        box_form_1.SetFlexibleDirection( wx.BOTH )
        box_form_1.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
        
        self.label_type = wx.StaticText( self, wx.ID_ANY, u"测试类型", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.label_type.Wrap( -1 )
        box_form_1.Add( self.label_type, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
        
        input_typeChoices = [ u"桌面机", u"开发机", u"内网", u"仿真", u"线上" ]
        self.input_type = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, input_typeChoices, 0 )
        self.input_type.SetSelection( 2 )
        box_form_1.Add( self.input_type, wx.GBPosition( 0, 2 ), wx.GBSpan( 1, 1 ), wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
        
        self.label_num_1 = wx.StaticText( self, wx.ID_ANY, u"第", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.label_num_1.Wrap( -1 )
        box_form_1.Add( self.label_num_1, wx.GBPosition( 0, 3 ), wx.GBSpan( 1, 1 ), wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
        
        input_numChoices = [ u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"10" ]
        self.input_num = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, input_numChoices, 0 )
        self.input_num.SetSelection( 0 )
        box_form_1.Add( self.input_num, wx.GBPosition( 0, 4 ), wx.GBSpan( 1, 1 ), wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
        
        self.label_num_2 = wx.StaticText( self, wx.ID_ANY, u"轮", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.label_num_2.Wrap( -1 )
        box_form_1.Add( self.label_num_2, wx.GBPosition( 0, 5 ), wx.GBSpan( 1, 1 ), wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
        
        input_tChoices = [ u"普通", u"回归" ]
        self.input_t = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, input_tChoices, 0 )
        self.input_t.SetSelection( 0 )
        box_form_1.Add( self.input_t, wx.GBPosition( 0, 1 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
        
        bSizer3.Add( box_form_1, 0, wx.EXPAND, 5 )
        
        box_form_2 = wx.FlexGridSizer( 0, 2, 0, 0 )
        box_form_2.SetFlexibleDirection( wx.BOTH )
        box_form_2.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
        
        self.label_jira = wx.StaticText( self, wx.ID_ANY, u"提案号", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.label_jira.Wrap( -1 )
        box_form_2.Add( self.label_jira, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
        
        self.input_jira = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 400,-1 ), 0 )
        box_form_2.Add( self.input_jira, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
        
        self.label_svn = wx.StaticText( self, wx.ID_ANY, u"SVN", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.label_svn.Wrap( -1 )
        box_form_2.Add( self.label_svn, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
        
        self.input_svn = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 400,-1 ), 0 )
        box_form_2.Add( self.input_svn, 0, wx.ALL, 5 )
        
        self.label_host = wx.StaticText( self, wx.ID_ANY, u"HOSTS", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.label_host.Wrap( -1 )
        box_form_2.Add( self.label_host, 0, wx.ALL, 5 )
        
        self.input_host = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 400,100 ), wx.TE_MULTILINE )
        box_form_2.Add( self.input_host, 0, wx.ALL, 5 )
        
        bSizer3.Add( box_form_2, 0, wx.EXPAND, 5 )
        
        box_btn = wx.BoxSizer( wx.VERTICAL )
        
        self.submit = wx.Button( self, wx.ID_ANY, u"开始生成", wx.Point( -1,-1 ), wx.DefaultSize, 0 )
        box_btn.Add( self.submit, 0, wx.ALL, 5 )
        
        bSizer3.Add( box_btn, 0, wx.EXPAND, 5 )
        
        self.SetSizer( bSizer3 )
        self.Layout()
        
        self.Centre( wx.BOTH )
        
        # Connect Events
        self.input_t.Bind( wx.EVT_CHOICE, self.choice_t )
        self.input_jira.Bind( wx.EVT_KILL_FOCUS, self.get_issue )
        self.input_svn.Bind( wx.EVT_KILL_FOCUS, self.get_svn_ver )
        self.submit.Bind( wx.EVT_BUTTON, self.make_mail )
    
    def __del__( self ):
        pass
    
    
    # 回归自动处理
    def choice_t( self, event ):
        selected_str = self.input_t.GetStringSelection();
        if selected_str == u"回归":
            self.input_svn.SetValue(env.svn_trunk)
            self.input_type.SetStringSelection("仿真")
            self.get_svn_ver(event)
    
    #获取提案内容(考虑变成多线程)
    def get_issue( self, event ):
        thread = threading.Thread(target=get_issue_data, args=[self.input_jira.GetValue()])
        thread.start()
        self.t["issue"] = thread
    
    #获取SVN版本号(考虑变成多线程)
    def get_svn_ver( self, event ):
        thread = threading.Thread(target=get_svn_ver_data, args=[self.input_svn.GetValue()])
        thread.start()
        self.t["svn"] = thread

    
    #发送邮件
    def make_mail( self, event ):
        global issue_info, svn_ver
        nl = "%0A%0C"
        
        #待相关线程执行结束
        for key in self.t:
            self.t[key].join()

        type = self.input_type.GetStringSelection();
        num = int(self.input_num.GetStringSelection());
        
        #基础
        selected_str = self.input_t.GetStringSelection();
        if selected_str == u"回归":
            if num == 1:
                subject = u"%s提交回归测试(%s)" % (issue_info["summary"], type)
            else :
                subject = u"%s提交第%d轮回归测试(%s)" % (issue_info["summary"], num, type)
        else:        
            subject = u"%s提交第%d轮测试(%s)" % (issue_info["summary"], num, type)
            
        body = subject + nl + nl

        #提案
        body += u"需求提案："+issue_info["url"]+nl+nl

        #SVN
        svn_url = self.input_svn.GetValue()
        if svn_url:            
            body += u"SVN地址："+svn_url+nl
            body += u"SVN版本号："+svn_ver+nl+nl
        
        #HOST
        host = self.input_host.GetValue()
        if host:
            body += u"HOSTS："+nl+urllib.quote(host)+nl+nl
        
        send_mail(env.mail_to, env.mail_cc, subject, body)


#获取提案内容，并写入全局变量
def get_issue_data(issue_code):
    global issue_info
    issue = str(issue_code)
    issue_info = {}
    if issue:            
        issue_info = show_issue(issue)
#        print "ISSUE URL:" + issue_info["url"]
#        print "ISSUE SUMMARY:" + issue_info["summary"]

#获取SVN版本号数据，并写入全局变量
def get_svn_ver_data(svn_url):
    global svn_ver
    
    svn_ver = 0
    if svn_url :
        svn_ver = str(svn.show_last_version(svn_url))
#        print "SVN VERSION:" + svn_ver

#初始化图形界面
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(parent=None)
        frame.Show(True)
        self.SetTopWindow(frame)
        return True



